package com.xbongbong.paas.script.controller;

import com.xbongbong.paas.script.pojo.WorkflowDingtalkUpdateDTO;
import com.xbongbong.paas.script.pojo.dto.DingtalkBatchUpdateDTO;
import com.xbongbong.paas.script.pojo.dto.DingtalkUpdateDTO;
import com.xbongbong.paas.service.dingtalkTodo.WorkflowDingtalkTodoService;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.workrecord.enums.WorkrecordResultEnum;
import com.xbongbong.pro.process.pojo.dto.WorkrecordUpdateTaskDTO;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * Fix 49962、50099、50140 钉钉待办审批通过之后还在待处理列表
 *
 * @author 李智康
 * @date 2022/07/05 14:33
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/script/workflow")
public class WorkFlowDingtalkUpdateController {

    @Resource
    private WorkflowDingtalkTodoService workflowDingtalkTodoService;

    /**
     * 更新钉钉事项状态
     *
     * @param workflowDingtalkUpdateDTO
     * @param br
     * @return
     */
    @RequestMapping("/dingtalkTaskUpdate")
    public String dingtalkTaskUpdate(@Valid @RequestBody WorkflowDingtalkUpdateDTO workflowDingtalkUpdateDTO, BindingResult br) {
        WorkrecordUpdateTaskDTO workrecordUpdateTaskDTO = new WorkrecordUpdateTaskDTO();
        BeanUtil.copyProperties(workflowDingtalkUpdateDTO, workrecordUpdateTaskDTO, false);
        workflowDingtalkTodoService.updateSingleDingtalkTodoTask(workrecordUpdateTaskDTO, WorkrecordResultEnum.AGREE);
        return "success";
    }

    /**
     * 更新钉钉实例状态
     *
     * @param workflowDingtalkUpdateDTO
     * @param br
     * @return
     */
    @RequestMapping("/dingtalkInstanceUpdate")
    public String dingtalkInstanceUpdate(@Valid @RequestBody WorkflowDingtalkUpdateDTO workflowDingtalkUpdateDTO, BindingResult br) {
        workflowDingtalkTodoService.updateDingtalkTodoInstance(workflowDingtalkUpdateDTO.getCorpid(),
                workflowDingtalkUpdateDTO.getDingtalkProcessInstanceId(), WorkrecordResultEnum.AGREE);
        return "success";
    }

    /**
     * 批量更新钉钉实例状态
     *
     * @param dingtalkBatchUpdateDTO
     * @param br
     * @return
     */
    @RequestMapping("/dingtalkInstanceBatchUpdate")
    public String dingtalkInstanceBatchUpdate(@Valid @RequestBody DingtalkBatchUpdateDTO dingtalkBatchUpdateDTO, BindingResult br) {
        for (DingtalkUpdateDTO dingtalkUpdateDTO : dingtalkBatchUpdateDTO.getList()) {
            workflowDingtalkTodoService.updateDingtalkTodoInstance(dingtalkUpdateDTO.getCorpid(),
                    dingtalkUpdateDTO.getDingtalkProcessInstanceId(), WorkrecordResultEnum.AGREE);
        }
        return "success";
    }

    /**
     * 批量取消待办
     *
     * @param workflowDingtalkUpdateDTO
     * @param br
     * @return
     */
    @RequestMapping("/dingtalkTaskGroupCancel")
    public String dingtalkTaskGroupCancel(@Valid @RequestBody WorkflowDingtalkUpdateDTO workflowDingtalkUpdateDTO, BindingResult br) {
        WorkrecordUpdateTaskDTO workrecordUpdateTaskDTO = new WorkrecordUpdateTaskDTO();
        workrecordUpdateTaskDTO.setCorpid(workflowDingtalkUpdateDTO.getCorpid());
        workrecordUpdateTaskDTO.setDingtalkInstanceId(workflowDingtalkUpdateDTO.getDingtalkProcessInstanceId());
        workrecordUpdateTaskDTO.setDingtalkActivityId(workflowDingtalkUpdateDTO.getDingtalkWorkrecordActivityId());
        workflowDingtalkTodoService.cancelDingtalkTodoTaskGroup(workrecordUpdateTaskDTO);
        return "success";
    }

}
